@extends('base')

@section('content')
<div class="layui-card">
    <div class="layui-card-body">
        <form class="layui-form layui-form-pane" action="javascript:void(0);">
            <div class="layui-form-item">
                <div class="layui-form-item layui-inline">
                    <label class="layui-form-label">用户名</label>
                    <div class="layui-input-inline">
                        <input type="text" name="name" placeholder="请输入搜索关键词" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item layui-inline">
                    <label class="layui-form-label">账号</label>
                    <div class="layui-input-inline">
                        <input type="text" name="account" placeholder="请输入搜索关键词" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item layui-inline">
                    <label class="layui-form-label">角色</label>
                    <div class="layui-input-inline">
                        <select name="rid" lay-verify="required|number">
                            <option value="0">--请选择角色--</option>@foreach( $role as $vo)
                            <option value="{{ $vo['id'] }}">{{ $vo['role_name'] }}</option>@endforeach
                        </select>
                    </div>
                </div>
                <div class="layui-form-item layui-inline">
                    <button class="pear-btn pear-btn-md pear-btn-primary" lay-submit lay-filter="account-query"><i class="layui-icon layui-icon-search"></i>查询</button>
                    <button type="reset" class="pear-btn pear-btn-md"><i class="layui-icon layui-icon-refresh"></i>重置</button>
                </div>
            </div>
        </form>
    </div>
</div>

<div class="layui-card">
    <div class="layui-card-body">
        <table class="user-table" id="account-table" lay-filter="account-table"></table>
    </div>
</div>

<script type="text/html" id="account-toolbar">
    <button class="pear-btn pear-btn-primary pear-btn-md" lay-event="add"><i class="layui-icon layui-icon-add-1"></i>新增</button>
</script>

<script type="text/html" id="account-bar">
    <button class="pear-btn pear-btn-primary pear-btn-sm" lay-event="edit"><i class="layui-icon layui-icon-edit"></i></button>
    <button class="pear-btn pear-btn-warming pear-btn-sm" lay-event="mod"><i class="layui-icon layui-icon-password"></i></button>
    <button class="pear-btn pear-btn-danger pear-btn-sm" lay-event="remove"><i class="layui-icon layui-icon-delete"></i></button>
</script>

<script type="text/html" id="account-status">
    <input type="checkbox" name="status" value="@{{ d.id }}" lay-skin="switch" lay-text="启用|禁用" lay-filter="account-status" @{{ d.account_status== 1 ? 'checked' : '' }} />
</script>

<script type="text/html" id="account-role">
    @{{ d.role.role_name }}
</script>

<script type="text/html" id="account-createTime">
  @{{  layui.util.toDateString(d.create_time, 'yyyy-MM-dd HH:mm:ss') }}
</script>
@endsection

@section('js')
<script>
    layui.use(['table', 'form', 'jquery','common'], function() {
        let table = layui.table;
        let form = layui.form;
        let $ = layui.jquery;
        let common = layui.common;

        let cols = [
            [
                {title: 'ID', field: 'id', align: 'center', width: 80},
                {title: '账号', field: 'account', align: 'center', width: 100},
                {title: '名称', field: 'account_name', align: 'center'},
                {title: '角色', field: 'role', align: 'center', templet: '#account-role'},
                {title: '电子邮箱', field: 'account_email', align: 'center'},
                {title: '手机号码', field: 'account_phone', align: 'center'},
                {title: '创建时间', field: 'create_time', align: 'center', templet: '#account-createTime'},
                {title: '状态', field: 'account_status', align: 'center', templet: '#account-status'},
                {title: '操作', toolbar: '#account-bar', align: 'center', width: 160}
            ]
        ]

        table.render({
            elem: '#account-table',
            url: '/account/list',
            page: true,
            cols: cols,
            skin: 'line',
            toolbar: '#account-toolbar',
            defaultToolbar: [{
                title: '刷新',
                layEvent: 'refresh',
                icon: 'layui-icon-refresh',
            }, 'filter', 'print', 'exports']
        });

        table.on('tool(account-table)', function(obj) {
            if (obj.event === 'remove') {
                window.remove(obj);
            } else if (obj.event === 'edit') {
                window.edit(obj);
            } else if (obj.event === 'mod') {
                window.mod(obj);
            }
        });

        table.on('toolbar(account-table)', function(obj) {
            if (obj.event === 'add') {
                window.add();
            } else if (obj.event === 'refresh') {
                window.refresh();
            }
        });

        form.on('submit(account-query)', function(data) {
            table.reload('account-table', {
                where: data.field
            })
        });

        form.on('switch(account-status)', function(obj) {
            $.ajax({
                url: "/account/status/" + this.value,
                dataType: 'json',
                type: 'get',
                // async: false,
                headers: {
                    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                },
                success: function(result) {
                    if (result.status) {
                        layer.msg(result.msg, {icon: 1, time: 1000}, function () {
                            table.reload('account-table');
                        })
                    } else {
                        layer.msg(result.msg, {icon: 2, time: 1000}, function (){
                            table.reload('account-table');
                        })
                    }
                }
            })
            return false
        });

        window.add = function() {
            layer.open({
                type: 2,
                title: '新增管理员',
                shade: 0.1,
                area: [common.isModile()?'100%':'500px', common.isModile()?'100%':'540px'],
                content: '/account/add'
            });
        }

        window.edit = function(obj) {
            layer.open({
                type: 2,
                title: '修改基本信息',
                shade: 0.1,
                area: [common.isModile()?'100%':'500px', common.isModile()?'100%':'460px'],
                content: '/account/edit/' + obj.data['id']
            });
        }

        window.mod = function(obj) {
            layer.open({
                type: 2,
                title: '修改登录密码',
                shade: 0.1,
                area: [common.isModile()?'100%':'500px', common.isModile()?'100%':'320px'],
                content: '/account/mod/' + obj.data['id']
            });
        }

        window.remove = function(obj) {
            layer.confirm('确定要删除吗？', {
                icon: 3,
                title: '提示'
            }, function(index) {
                layer.close(index);
                let loading = layer.load();
                $.ajax({
                    url: "/account/delete/" + obj.data['id'],
                    dataType: 'json',
                    type: 'delete',
                    headers: {
                        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                    },
                    success: function(result) {
                        layer.close(loading);
                        if (result.status) {
                            layer.msg(result.msg, {icon: 1, time: 1000}, function() {
                                obj.del();
                            });
                        } else {
                            layer.msg(result.msg, {icon: 2, time: 1000});
                        }
                    }
                })
                return false
            });
        }

        window.refresh = function(param) {
            table.reload('account-table');
        }
    })
</script>
@endsection


